import { Component, OnInit } from '@angular/core'
import { TableWidthConfig } from 'ng-devui/data-table'
import { ManageService } from 'src/app/@core/manage.service'
import { DialogService } from 'ng-devui/modal'
import { DelectComponent } from './delect/delect.component'
import { FormLayout } from 'ng-devui'

@Component({
  selector: 'app-user-manage',
  templateUrl: './user-manage.component.html',
  styleUrls: ['./user-manage.component.scss']
})
export class UserManageComponent implements OnInit {
  layoutDirection: FormLayout = FormLayout.Horizontal

  basicDataSource = [{}]

  id = {
    userid: null
  }

  datas = { role: null, userid: null, name: null, locked: null }

  dataTableOptions = {
    columns: [
      {
        field: 'userName',
        header: '用户名',
        fieldType: 'text'
      },
      {
        field: 'id',
        header: '用户ID',
        fieldType: 'text'
      },
      {
        field: 'role',
        header: '用户身份',
        fieldType: 'text'
      },
      {
        field: 'authenticated',
        header: '实名认证',
        fieldType: 'text'
      },
      {
        field: 'locked',
        header: '状态',
        fieldType: 'text'
      },
      {
        field: 'operation',
        header: '操作'
      }
    ]
  }

  tableWidthConfig: TableWidthConfig[] = [
    {
      field: 'userName',
      width: '150px'
    },
    {
      field: 'id',
      width: '150px'
    },
    {
      field: 'role',
      width: '150px'
    },
    {
      field: 'locked',
      width: '150px'
    },
    {
      field: 'operation',
      width: '150px'
    }
  ]

  constructor(
    private manageService: ManageService,
    private dialogService: DialogService
  ) { }

  ngOnInit(): void {
    this.getList()
  }

  refresh() {
    this.getList()
  }

  onSearch(term) {
    if (term != '') {
      this.id.userid = term
      this.getListBaseOnId()
      this.id.userid = null
    }
  }

  pager = {
    pagenum: 1,
    pagesize: 10,
    username: null,
    locked: null
  }

  totalPageNum = 0
  totalNum = 0
  preLink = '<span class="icon-arrow-left"></span>'
  nextLink = '<span class="icon-arrow-right"></span>'

  pageIndexChange(pageIndex) {
    this.pager.pagenum = pageIndex
    this.getList()
  }
  pageSizeChange(pageSize) {
    this.pager.pagesize = pageSize
    this.pageIndexChange(1)
  }

  getList() {
    this.manageService.getUserList(this.pager).subscribe((res) => {
      this.basicDataSource = res.data.userLists
      this.totalNum = res.data.totalNum
      this.totalPageNum = res.data.totalPageNum
    })
  }

  getListBaseOnId() {
    this.manageService.getUserListBaseOnID(this.id).subscribe((res) => {
      if (res.code == 0) {
        this.basicDataSource = [res.data]
      } else {
        this.openDialog('failed', res.msg)
      }
    })
  }

  config = {
    id: 'dialog-service',
    width: '346px',
    maxHeight: '600px',
    zIndex: 1050,
    backdropCloseable: true,
    html: true
  }

  ConfirmDeletedUserName: string

  deleteUser(userid?: number, userName?: string) {
    this.id.userid = userid
    this.openDeleteDialog('standard', userName)
  }

  openDialog(dialogtype?: string, content?: string) {
    const results = this.dialogService.open({
      ...this.config,
      dialogtype: dialogtype,
      content: content,
      buttons: [
        {
          cssClass: 'primary',
          text: '关闭',
          handler: ($event: Event) => {
            results.modalInstance.hide()
          }
        }
      ]
    })
  }

  lockUser(userid?: number, locked?: boolean, userName?: string) {
    this.id.userid = userid
    if (locked == false) {
      this.openLockDialog('warning', '请确认是否锁定' + userName + '?', locked)
    } else {
      this.openLockDialog('warning', '请确认是否解锁' + userName + '?', locked)
    }
  }

  judge(flag: number, msg: string) {
    if (flag == 0) {
      this.openDialog('success', msg)
      this.getList()
    } else {
      this.openDialog('failed', msg)
    }
  }

  openDeleteDialog(dialogtype?: string, userName?: string) {
    const results = this.dialogService.open({
      id: 'dialog-service',
      width: '600px',
      maxHeight: '600px',
      title: '删除账号',
      backdropCloseable: true,
      dialogtype: dialogtype,
      content: DelectComponent,
      buttons: [
        {
          cssClass: 'primary',
          text: '确认',
          disabled: true,
          handler: ($event: Event) => {
            if (results.modalContentInstance.userName == userName) {
              this.manageService.deleteUser(this.id).subscribe((res) => {
                this.judge(res.code, res.msg)
              })
              results.modalInstance.hide()
            } else {
              this.openDialog(
                'failed',
                '输入用户名与被选用户不一致，用户删除失败！'
              )
            }
          }
        },
        {
          cssClass: 'primary',
          text: '关闭',
          handler: ($event: Event) => {
            results.modalInstance.hide()
          }
        }
      ],
      data: {
        canConfirm: (value: boolean) => {
          results.modalInstance.updateButtonOptions([{ disabled: !value }])
        }
      }
    })
  }

  openLockDialog(dialogtype?: string, content?: string, locked?: boolean) {
    const results = this.dialogService.open({
      ...this.config,
      dialogtype: dialogtype,
      content: content,
      buttons: [
        {
          cssClass: 'primary',
          text: '确认',
          handler: ($event: Event) => {
            if (locked == false) {
              this.manageService.lockUser(this.id).subscribe((res) => {
                this.judge(res.code, res.msg)
              })
            } else {
              this.manageService.unlockUser(this.id).subscribe((res) => {
                this.judge(res.code, res.msg)
              })
            }
            results.modalInstance.hide()
          }
        },
        {
          text: '关闭',
          cssClass: 'primary',
          handler: ($event: Event) => {
            results.modalInstance.hide()
          }
        }
      ]
    })
  }

  options2 = [
    {
      name: '锁定',
      value: true
    },
    {
      name: '正常',
      value: false
    }
  ]

  currentOption4 = { value: null }

  submitForm(term) {
    if (this.currentOption4 === null) {
      this.currentOption4 = { value: null }
    }
    this.pager.locked = this.currentOption4.value
    this.pager.pagenum = 1
    this.getList()
  }

  restForm() {
    this.currentOption4 = { value: null }
    this.pager = {
      pagenum: 1,
      pagesize: this.pager.pagesize,
      username: null,
      locked: null
    }
    this.getList()
  }
}
